export function parseData (fileReader) {
  const arrayBuffer = fileReader.result // 获取读取的 ArrayBuffer
  // 创建一个 DataView 来读取和解析 ArrayBuffer
  const dataView = new DataView(arrayBuffer)

  // 使用 DataView 的各种方法来读取数据
  // console.log('dataView', dataView)

  // 标识位 4字节
  // const int32Value = dataView.getInt32(0)
  // type 0快 1慢
  const type = dataView.getInt8(4)
  // ip 4字节  port 4字节
  // 注：dataView.getInt8() 方法返回的结果是按照 二进制补码形式 表示的，最高位为符号位，如果需要解析为十进制数，可以加一个判断，如果是负数 则 & 0xff 将负数转换为无符号数
  const url = `${
          dataView.getInt8(5) < 0
            ? dataView.getInt8(5) & 0xff
            : dataView.getInt8(5)
        }.${
          dataView.getInt8(6) < 0
            ? dataView.getInt8(6) & 0xff
            : dataView.getInt8(6)
        }.${
          dataView.getInt8(7) < 0
            ? dataView.getInt8(7) & 0xff
            : dataView.getInt8(7)
        }.${
          dataView.getInt8(8) < 0
            ? dataView.getInt8(8) & 0xff
            : dataView.getInt8(8)
        }:${dataView.getInt32(9)}`
        // originStart
  const originStart = dataView.getFloat32(13)
  // originEnd
  const originEnd = dataView.getFloat32(17)
  // startFreq
  const startFreq = dataView.getFloat32(21)
  // endFreq
  const endFreq = dataView.getFloat32(25)
  // temprature
  const temprature = dataView.getFloat32(29)
  // dian
  const dian = dataView.getFloat32(33)
  // pointNum
  const pointNum = dataView.getInt32(37)
  // time
  const time = dataView.getInt32(41)
  // 数据类型
  const dataType = dataView.getInt16(45)
  // 长度
  const length = dataView.getInt16(47) < 0 ? dataView.getInt16(47) & 0xffff : dataView.getInt16(47)

  // fFTAvg
  // const fFTAvg = []
  const fFTRealtime = [] // new Float32Array(dataView.buffer, 40, length)
  const fFTMax = []
  const fFTMin = []
  for (let index = 49; index < 49 + 4 * length; index += 4) {
    // fFTAvg.push(dataView.getFloat32(index))
    fFTRealtime.push(dataView.getFloat32(index))
  }
  let pubuLength = 0
  const pubuData = []

  // #region
  /* if (this.fFTMax.length === 0 &&
                      this.fFTMin.length === 0) {
            this.fFTMax = [...fFTRealtime]
            this.fFTMin = [...fFTRealtime]
          }

          fFTRealtime.forEach((item, index) => {
            if (this.fFTMax[index] < item) this.fFTMax[index] = item
            if (this.fFTMin[index] > item) this.fFTMin[index] = item
          }) */
  // #endregion
  switch (dataType) {
    case 3:
      for (
        let index = 49 + 4 * length;
        index < 49 + 8 * length;
        index += 4
      ) {
        fFTMax.push(dataView.getFloat32(index))
      }
      if (type != 1) {
        pubuLength = dataView.getInt16(49 + 8 * length)
        for (
          let index = 51 + 8 * length;
          index < 51 + 8 * length + 4 * pubuLength;
          index += 4
        ) {
          pubuData.push(dataView.getFloat32(index))
        }
      }

      break

    case 5:
      for (
        let index = 49 + 4 * length;
        index < 49 + 8 * length;
        index += 4
      ) {
        fFTMin.push(dataView.getFloat32(index))
      }
      if (type != 1) {
        pubuLength = dataView.getInt16(49 + 8 * length)
        for (
          let index = 51 + 8 * length;
          index < 51 + 8 * length + 4 * pubuLength;
          index += 4
        ) {
          pubuData.push(dataView.getFloat32(index))
        }
      }

      break
    case 7:
      for (
        let index = 49 + 4 * length;
        index < 49 + 8 * length;
        index += 4
      ) {
        fFTMax.push(dataView.getFloat32(index))
      }
      for (
        let index = 49 + 8 * length;
        index < 49 + 12 * length;
        index += 4
      ) {
        fFTMin.push(dataView.getFloat32(index))
      }
      if (type != 1) {
        pubuLength = dataView.getInt16(49 + 12 * length)
        for (
          let index = 51 + 12 * length;
          index < 51 + 12 * length + 4 * pubuLength;
          index += 4
        ) {
          pubuData.push(dataView.getFloat32(index))
        }
      }

      break

    default:
      if (type != 1) {
        pubuLength = dataView.getInt16(49 + 4 * length)
        for (
          let index = 51 + 4 * length;
          index < 51 + 4 * length + 4 * pubuLength;
          index += 4
        ) {
          pubuData.push(dataView.getFloat32(index))
        }
      }
      break
  }

  return {
    type,
    url,
    originStart,
    originEnd,
    startFreq,
    endFreq,
    temprature,
    dian,
    pointNum,
    time,
    dataType,
    length,
    fFTAvg: [...fFTRealtime],
    fFTRealtime,
    fFTMax,
    fFTMin,
    pubuLength,
    pubuData
    // fFTMax: this.fFTMax,
    // fFTMin: this.fFTMin
  }
}

export function parseJsonData (eData, dom) {
  if (!dom) return
  const data = JSON.parse(eData)
  // console.log('ws推送数据', data)
  if (data.fileSize) {
    dom.fileSize = data.fileSize
  }
  if (data.saveStatus !== undefined) {
    dom.isSaving = data.saveStatus
  }

  if (data.pubuMax !== undefined && data.pubuMin !== undefined) {
    // dom.pubuMax = data.pubuMax
    dom.pubuMinFull = data.pubuMin
    // dom.changeYRange(data.pubuMax - 10, data.pubuMin)
  }

  if (data.signalList !== undefined) {
    dom.zaiboList = data.signalList
    if (dom.showZaibo) {
      dom.$refs.specView.series.clearXmarkZone()
      data.signalList.forEach((item) => {
        // console.log('item', item)
        const start = 1000 * (item.center * 1 - item.bandwidth / 2)
        const end = 1000 * (item.center * 1 + item.bandwidth / 2)
        const str = '' // item.center

        if (dom.selectedRow && dom.selectedRow.center == item.center) {
          dom.$refs.specView.series.addXMarkZoneFocus(start, end, str, {
            r: 212 / 255,
            g: 74 / 255,
            b: 62 / 255,
            a: 0.5
          })
        } else {
          dom.$refs.specView.series.addXMarkZoneFocus(start, end, str, {
            r: 255 / 255,
            g: 255 / 255,
            b: 255 / 255,
            a: 0.5
          })
        }
      })
    }

    // dom.currentPage = 1
  }
}

export function detailInfo (eData, dom) {
  if (!dom || (!dom.isPlay && dom.getNewData)) return
  // 弹窗里详细信息
  if (dom &&
        dom.showDetialDialog) {
    const chartObj = {}
    if (eData.fFTRealtime) {
      chartObj.pubuData = eData.pubuData
      chartObj.fFTRealtime = eData.fFTRealtime
    }

    if (
      eData.fFTAvg &&
          dom.detectType.includes('2')
    ) {
      chartObj.fFTAvg = eData.fFTAvg
    }
    if (
      eData.fFTMax &&
          dom.detectType.includes('3')
    ) {
      chartObj.fFTMax = eData.fFTMax
    }
    if (
      eData.fFTMin &&
          dom.detectType.includes('4')
    ) {
      chartObj.fFTMin = eData.fFTMin
    }

    dom.$refs.specView &&
          dom.drawChart(
            chartObj,
            eData.startFreq,
            eData.endFreq,
            eData.originStart,
            eData.originEnd
          )

    if (eData.time !== undefined) {
      dom.scanTime = eData.time
    } else {
      dom.scanTime = null
    }
    if (eData.pointNum !== undefined) {
      dom.realPointNum = eData.pointNum
    } else {
      dom.realPointNum = null
    }

    if (eData.dian !== undefined && eData.temprature !== undefined) {
      dom.dian = eData.dian
      dom.temprature = eData.temprature
    } else {
      dom.dian = null
      dom.temprature = null
    }

    if (dom.getNewData == false) {
      dom.getNewData = true
      // console.log('接收到新数据了')
    }
  }
}
